package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.wholesale.beans.GoodsInfo;
import com.wholesale.datas.DbHelperAdapter;
import com.wholesale.utils.GoodsListAdapter;

public class HistoryLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private RelativeLayout loadingLayout;

	private GoodsListAdapter listAdapter;
	private HistroyListAsync histroyListAsync;

	// 构造函数
	public HistoryLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.common, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("More");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setText("Clear");
		rightButton.setOnClickListener(this);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("History");
		// item 单击事件
		bodyList.setOnItemClickListener(itemClieckListener);
		bodyList.setSelector(R.drawable.product_list_bg);
		listAdapter = new GoodsListAdapter(context, bodyList);
		bodyList.setAdapter(listAdapter);
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);
		histroyListAsync = new HistroyListAsync(context);
		histroyListAsync.execute(false);
	}

	// ListView 点击事件
	OnItemClickListener itemClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			GoodsDetailLayout goodsDetailLayout = new GoodsDetailLayout(
					context, viewFlipper, "History", listAdapter.getItem(arg2));
			viewFlipper.addView(goodsDetailLayout);
			viewFlipper.showNext();
		}
	};

	class HistroyListAsync extends
			AsyncTask<Boolean, Integer, ArrayList<GoodsInfo>> {
		boolean desc;

		public HistroyListAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected ArrayList<GoodsInfo> doInBackground(Boolean... params) {
			desc = params[0];
			ArrayList<GoodsInfo> result = new ArrayList<GoodsInfo>();
			DbHelperAdapter dbHelperAdapter = new DbHelperAdapter(context);
			dbHelperAdapter.open();
			if (desc) {
				dbHelperAdapter.deleteHistoryAll();
			} else {
				result = dbHelperAdapter.getAllHistory();
			}
			dbHelperAdapter.close();
			return result;
		}

		@Override
		protected void onPostExecute(ArrayList<GoodsInfo> result) {
			if (desc) {
				Toast.makeText(context, "Clear successfully!", 500).show();
			}
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			histroyListAsync = new HistroyListAsync(context);
			histroyListAsync.execute(true);
			break;
		default:
			break;
		}

	}
}
